/*
 * Copyright (c) gitee.com/jiandong001.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { View } from './uimanager/View';
import { ReactShadowNode } from './uimanager/ReactShadowNode';

import List from '@ohos.util.List';
import hilog from '@ohos.hilog';
import ArrayList from '@ohos.util.ArrayList';
import HashMap from '@ohos.util.HashMap';

import { ReactTextManager } from './views/text/ReactTextManager';
import { ReactImageManager } from './views/image/ReactImageManager';
import { ReactViewManager } from './views/view/ReactViewManager';
import { ViewManager } from './uimanager/ViewManager';
import { UIImplementationProvider } from './uimanager/UIImplementationProvider';
import { ReactModuleInfo } from './modules/model/ReactModuleInfo';
import { UIManagerModule } from './uimanager/UIManagerModule';
import { NativeModuleRegistry } from './bridge/NativeModuleRegistry';
import { ReactShadowNodeViewManager } from './uimanager/ViewManagerRegistry'
import { ReactPackage } from './ReactPackage';
import { ReactInstanceManager } from './ReactInstanceManager';
import { ReactContext } from './bridge/ReactContext';
import { ModuleHolder } from './bridge/ModuleHolder'

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */


/** Helper class to build NativeModuleRegistry. */
export class NativeModuleRegistryBuilder {
  private static readonly TAG = "React NativeModuleRegistryBuilder"
  private mModules = new HashMap<string, ModuleHolder>();
  private mViewManagers = new ArrayList<ReactShadowNodeViewManager>();
  //TODO：just temperate solution, should delete mUIModule and merged to mModules
  private mUIModule: UIManagerModule

  public constructor(
    private mReactContext: ReactContext, private mReactInstanceManager: ReactInstanceManager) {
  }

  public processPackage(reactPackage: ReactPackage): void {

    hilog.info(0x0000, NativeModuleRegistryBuilder.TAG, 'processPackage')

    //todo: just enabel uimanager modlue
    // let uiInfo = new ReactModuleInfo("UIManager", "UIManager", false, false, false, false, true);
    //this.mModules.set("uiManager", new ModuleHolder(uiInfo, new UIImplementationProvider(null, new ViewManagerRegistry(), new UIViewOperationQueue  0)));
    this.mUIModule = new UIManagerModule(null, null, new UIImplementationProvider(), 0)

    hilog.info(0x0000, NativeModuleRegistryBuilder.TAG, 'processPackage22222')
    //    let moduleHolderIter: IterableIterator<[string, ModuleHolder]>;
    //
    //    if (reactPackage instanceof LazyReactPackage) {
    ////      moduleHolderIter =
    ////          ((LazyReactPackage) reactPackage).getNativeModuleIterator(reactPackage);
    //    } else if (reactPackage instanceof TurboReactPackage) {
    //      moduleHolderIter =
    //          ((TurboReactPackage) reactPackage).getNativeModuleIterator(reactPackage);
    //    } else {dui
    ////      moduleHolderIter =
    ////          ReactPackageHelper.getNativeModuleIterator(
    ////              reactPackage, this.mReactContext, this.mReactInstanceManager);
    //    }
    //
    //    let temp = moduleHolderIter.next().value;
    //    while(temp != undefined) {
    //        console.log("key:" + temp[0]);
    //        console.log("moduleHolder:" + (ModuleHolder)temp[1].;
    //        temp = iter.next().value;
    //
    //        let name = moduleHolder.getName();
    //        if (this.mModules.hasKey(name)) {
    //          existingNativeModule: ModuleHolder = mModules.get(name);
    //          if (!moduleHolder.getCanOverrideExistingModule()) {
    //            throw new Error(
    //                "Native module "
    //                    + name
    //                    + " tried to override "
    //                    + existingNativeModule.getClassName()
    //                    + ". Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExistingModule=true. "
    //                    + "This error may also be present if the package is present only once in getPackages() but is also automatically added later during build time by autolinking. Try removing the existing entry and rebuild.");
    //          }
    //          mModules.remove(existingNativeModule);
    //        }
    //        mModules.put(name, moduleHolder);
    //    }

  }

  getOrCreateViewManagers(reactContext: ReactContext): ArrayList<ViewManager<View, ReactShadowNode>> {
    if (!this.mViewManagers.length) return this.mViewManagers;

    //          this.mViewManagers.add(new ReactHorizontalScrollViewManager());
    //          this.mViewManagers.add(new ReactHorizontalScrollContainerViewManager());
    //          this.mViewManagers.add(new ReactScrollViewManager());
    //          // Native equivalents
    //          this.mViewManagers.add(new FrescoBasedReactTextInlineImageViewManager());
    this.mViewManagers.add(new ReactImageManager());
    //          this.mViewManagers.add(new ReactRawTextManager());
    //          this.mViewManagers.add(new ReactTextInputManager());
    this.mViewManagers.add(new ReactTextManager());
    this.mViewManagers.add(new ReactViewManager());
    //          this.mViewManagers.add(new ReactVirtualTextViewManager());
    //
    //          this.mViewManagers.add(new ReactUnimplementedViewManager());
    return this.mViewManagers;
  }

  public build(): NativeModuleRegistry {
    //TODO：just temperate solution, should delete mUIModule and merged to mModules
    hilog.info(0x0000, NativeModuleRegistryBuilder.TAG, 'build')
    return new NativeModuleRegistry(this.mReactContext, this.mModules, this.mUIModule);
  }
}
